package com.example.demo77.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo77.entity.Admin;
import com.example.demo77.entity.AdminQuery;
import com.example.demo77.entity.common.CommonResult;
import com.example.demo77.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;

/**
 * (Admin)表控制层
 *
 * @author makejava
 * @since 2020-05-25 20:18:57
 */
@Controller
@RequestMapping("admin")
public class AdminController {

    private Logger logger = LoggerFactory.getLogger(getClass());
    /**
     * 服务对象
     */
    @Resource
    private AdminService adminService;






    @RequestMapping("toAdd")
    public String toAdd(Model model){
        model.addAttribute("admin", new Admin());
        return "/user/admin_add";
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("getById")
    @ResponseBody
    public Admin getById(Integer id) {

        return this.adminService.getById(id);
    }

    @PostMapping("save")
    @ResponseBody
    public Object save(Admin admin) {
        Boolean result = false;
        if(admin.getId() != null){
            //编辑
            result = adminService.update(admin) > 0;
        }else{
            //添加
            admin.setRegDate(new Date());
            result = adminService.insert(admin) >0;
        }

        return result;
    }
    /*@PostMapping("edit")
    @ResponseBody
    public Boolean edit(Admin admin) {
        return adminService.update(admin) > 0;
    }

    @PostMapping("add")
    @ResponseBody
    public Admin add(Admin admin) {
        admin.setRegDate(new Date());
        return adminService.insert(admin);
    }*/

    @PostMapping("deleteById")
    @ResponseBody
    public Boolean deleteById(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return false;
        }
        adminService.deleteById(Arrays.asList(ids));
        return true;
    }

    @PostMapping("queryAll")
    @ResponseBody
    public Object queryAll(Integer page, Integer limit, AdminQuery admin){
        CommonResult<Admin> result = new CommonResult<>();
        IPage<Admin> iPage = adminService.queryAllByLimit(page, limit, admin);
        result.setCode(0);
        result.setCount(iPage.getTotal());
        result.setData(iPage.getRecords());
        return result;

    }

    @PostMapping("login")
    public String login(String account, String password, Model model, HttpSession session){
        Admin admin = adminService.login(account,password);
        session.setAttribute("loginAccount", admin.getAccount());
        if(admin==null){
            model.addAttribute("msg", "账号或密码错误");
            return "login";
        }
        return "index";
    }

    @RequestMapping("toLogin")
    public String toLogin(){
        logger.trace("trace是低级别");
        logger.debug("老二");
        logger.info("老三");
        logger.warn("老四");
        logger.error("老五");
        return "login";
    }

    @RequestMapping("toEdit")
    public String toEdit(Integer id,Model model){
        System.out.println("id==="+id);
        Admin admin = adminService.getById(id);
        model.addAttribute("admin",admin);
        return "/user/admin_add";
    }

    @RequestMapping("toWelcome")
    public String toWelcome(){
        return "welcome";
    }


    @RequestMapping("toList")
    public String toList(){
        return "/user/admin_list";
    }




}